<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/5/16
 * Time: 17:13
 */

namespace app\index\base_controller;

use app\index\controller\Login;
use think\Controller;
use think\Db;
use think\Exception;
use think\facade\Env;
use think\facade\Session;
use think\Log;

class BaseController extends Controller
{
    public function testPath(){
        dump(request()->url(true));
        dump( request()->path() );
        dump(request()->pathInfo());
        dump(request()->isMobile());
        dump(request()->module());
        dump(request()->controller());
        dump(request()->controller(true));
        dump(request()->action());
        dump(request()->action(true));
        dump(Env::get('extend_path'));
    }

    protected function initialize()
    {
        $userId = Login::checkLogin();
        if($userId === false){
            $this->redirect(url('index/login/login2'));
            die();
        }
        $this->assign('userId',$userId);
        $userName = Login::getUserName();
        $this->assign('sessionUserName',$userName);

        $menuList = $this->getUserMenuList();
        $this->assign('menu',$menuList);
    }

    public function getUserMenuList(){
        $userId = Login::getUserId();
        $userRoles = Db::table('MNG_GZ_USER_ROLE UR')
            ->field('UR.*,R.NAME,R.RULES')
            ->join('MNG_GZ_ROLE R','UR.ROLE_ID=R.ROLE_ID')
            ->where(['USER_ID'=>$userId])
            ->order(['LAST_LOGIN_TIME DESC'])
            ->select();
        $roleId = Login::getRoleId();
        foreach ($userRoles as $value){
            if($roleId == $value['ROLE_ID']){
                $role = $value;
            }
        }
        if(empty($role)){
            $role = $userRoles[0];
            $roleId = $role['ROLE_ID'];
            Login::setRoleId($roleId);
        }
        if(empty($role)){
            die('用户无该角色');
        }
        Db::table('MNG_GZ_USER_ROLE')->where(['USER_ID'=>$userId,'ROLE_ID'=>$roleId])->update(['LAST_LOGIN_TIME'=>time()]);

        $this->assign('userRoles',$userRoles);
        $this->assign('role',$role);

        $pageStr = $role['RULES'];
        if(empty($pageStr)){
            $pageStr = "''";
        }

        $pageList = Db::table('MNG_GZ_PAGE')
            ->where("GROUP_ID = 0 and (PAGE_ID in ($pageStr) or IS_BASE = 1)")
            ->order("DIS_ORDER ASC")
            ->select();

        $path = request()->path();
        $showNum = 0;
        foreach ($pageList as $key=>$page){
            $groupId = $page['PAGE_ID'];
            $list2 = Db::table('MNG_GZ_PAGE')
                ->where(" GROUP_ID=$groupId and (PAGE_ID in ($pageStr) or IS_BASE=1)")
                ->order('DIS_ORDER ASC')
                ->select();

            $pageList[$key]['show'] = 0;
            if( strtolower( $page['PAGE_URL']) == strtolower($path) ){
                $pageList[$key]['show'] = 1;
                Login::setGroupShowKey($key);
                $showNum = 1;
            }
            foreach ($list2 as $key2=>$page2){
                if( strtolower($page2['PAGE_URL']) == strtolower($path)){
                    $pageList[$key]['show'] = 1;
                    $list2[$key2]['show'] = true;
                    Login::setGroupShowKey($key);
                    Login::setPageShowKey($key2);
                    $showNum = 1;
                }
            }
            $pageList[$key]['menu2'] = $list2;
        }

        if ($showNum == 0) {
            $groupShowKey = Login::getGroupShowKey();
            $pageShowKey = Login::getPageShowKey();
            $pageList[$groupShowKey]['show'] = 1;
            $pageList[$groupShowKey]['menu2'][$pageShowKey]['show'] = true;
        }

        return $pageList;
    }



    public function fc_log() {
        $ip = $this->request->ip();
        $module = $this->request->module();
        $controller = $this->request->controller();
        $action = $this->request->action();
        $url = $this->request->pathinfo();
        $input = json_encode(input());

        $data['IP_NAME'] = $ip;
        $data['MODULE_NAME'] = $module;
        $data['CONTROLLER_NAME'] = $controller;
        $data['ACTION_NAME'] = $action;
        $data['URL_NAME'] = $url;
        $data['INPUT_CONTENT'] = $input;
        $data['CREATED_BY'] = Session::get('userId');
        $data['CREATED_ON'] = date('Y-m-d H:i:s');

        Db::table('fc_log')
            ->insert($data);
    }

    public function getDownLoadList($total) {
        $everyCount = 2000;
        $arr = [];
        if ($total<=$everyCount) {
            $arr[] = ['start'=>1,'end'=>$total];
        } else {
            $fullTotal =  floor($total/$everyCount);
            $left =  $total%$everyCount;
            for ($i = 1; $i<=$fullTotal; $i++) {
                $start = ($i-1)*$everyCount +1;
                $end = $start+$everyCount-1;
                $arr[] = ['start'=>$start,'end'=>$end];
            }
            if ($left>0) {
                $start = $fullTotal*$everyCount+1;
                $end =  $start+$left-1;
                $arr[] = ['start'=>$start,'end'=>$end];
            }
        }
        return $arr;
    }
}